{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "###########调包\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import *\n",
    "import time\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "############数据文件文件路径\n",
    "train_dir = '../../contest/train/'\n",
    "test_dir = '../../contest/A榜/'\n",
    "train_pickle_dir = './pickle/train/'\n",
    "test_pickle_dir = './pickle/A/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工企业存款():\n",
    "    res = []\n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(test_dir,test_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            企业存款_T0 = pd.read_csv(os.path.join(train_dir,'XW_DP_CUST_SUM.csv'))  \n",
    "        else:\n",
    "            企业存款_T0 = pd.read_csv(os.path.join(test_dir,'XW_DP_CUST_SUM_A.csv'))\n",
    "        企业存款_T0.columns = ['数据日期','客户ID','账户数','人民币金额']\n",
    "\t\t\n",
    "        企业存款_T0['人民币金额'] = pow((企业存款_T0['人民币金额'])/3.12,3).round(2)\n",
    "\n",
    "        企业存款_T0['数据日期'] = 企业存款_T0['数据日期'].astype('str')\n",
    "        企业存款_T0['数据日期'] = 企业存款_T0['数据日期'].astype('datetime64[ns]')\n",
    "        企业存款_T0['数据日期']=pd.to_datetime(企业存款_T0['数据日期'])+pd.DateOffset(days=11886)\n",
    "        企业存款_T0.sort_values(['客户ID','数据日期'],inplace=True,ascending=True)\n",
    "\t\t\n",
    "        企业存款_T1=企业存款_T0.groupby(['客户ID','数据日期']).agg({'人民币金额':['sum'],'账户数':['max']})\n",
    "        企业存款_T1.reset_index(inplace=True)\n",
    "        企业存款_T1.columns = ['客户ID','数据日期','人民币金额','账户数']\n",
    "\n",
    "        企业存款_T1['存款季比']=(企业存款_T1.groupby(['客户ID'])['人民币金额'].diff(3)*100/\\\n",
    "\t\t(企业存款_T1.groupby(['客户ID'])['人民币金额'].shift(3)+0.1)).round(0)\n",
    "        企业存款_T1['存款年比']=(企业存款_T1.groupby(['客户ID'])['人民币金额'].diff(12)*100/\\\n",
    "\t\t(企业存款_T1.groupby(['客户ID'])['人民币金额'].shift(12)+0.1)).round(0)\n",
    "\n",
    "        企业存款_T1['账户数环比']=企业存款_T1.groupby(['客户ID'])['账户数'].diff(1)\n",
    "\t\t\n",
    "        企业存款_T2=企业存款_T1.groupby(['客户ID']).agg({'人民币金额':['last','std','mean'],'账户数':['last'],'账户数环比':['sum']\\\n",
    "\t\t,'存款季比':['last','mean'],'存款年比':['last']})\n",
    "        企业存款_T2.reset_index(inplace=True)\n",
    "        企业存款_T2.columns = ['客户ID','企业最新存款','企业存款_std','企业存款_mean','企业存款账户数','企业存款账户数变化',\\\n",
    "\t\t'企业存款季比_last','企业存款季比_mean','企业存款年比_last']\n",
    "\t\t\n",
    "        企业存款_T2['企业存款_标准偏差'] = (100*企业存款_T2['企业存款_std']/企业存款_T2['企业存款_mean']).round(0)\n",
    "        企业存款_T2['企业存款_标准偏差'] = 企业存款_T2['企业存款_标准偏差'].replace([np.inf,-np.inf],np.nan)\n",
    "\n",
    "        if data_dir==train_dir:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(train_dir,'XW_TARGET.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户列表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(test_dir,'XW_TARGET_A.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\n",
    "        企业存款特征=目标客户列表.merge(企业存款_T2,on=['客户ID'],how='left')\n",
    "        企业存款特征.drop(['借款合同编号','纳税人识别号','法定代表人客户ID'],axis=1,inplace=True)\n",
    "\n",
    "        pickle.dump(企业存款特征, open(pickle_dir+'企业存款特征.p', 'wb'))\n",
    "        res.append(企业存款特征)\n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "企业存款_训练集,企业存款_测试集=加工企业存款()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 10)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "企业存款_训练集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5061, 10)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "企业存款_测试集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
